import netCDF4 as nc
import matplotlib.pyplot as plt
import numpy as np

# 打开下载的.nc文件
nc_file = nc.Dataset(r'D:\Users\Jiudian\Desktop\tmp\tmp_2022.nc', 'r')  # 'r'表示只读模式

# print(nc_file.variables.keys()) #dict_keys(['wy', 'lon', 'lat', 'time'])
latitude = nc_file.variables['lat'][:] # latitude与longtitude都是一维数组
longitude = nc_file.variables['lon'][:]
X, Y = np.meshgrid(longitude, latitude) # 合并成二维矩阵
time = nc_file.variables['time'][:]
wy = nc_file.variables['tmp'] # E是三维数组

print(longitude.shape,latitude.shape)
print(X.shape, Y.shape, wy.shape) #查看数组格式

plt.contourf(X, Y,  wy[11, :, :]) # 1981-2022, 42年，# 该文件经纬度是反的, 所以要转置
plt.colorbar(label="wy", orientation="horizontal")  # horizontal, vertical
plt.show()
nc_file.close() # 关闭nc文件